package com.dataStructure;

public class TreeTest {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(10);
        TreeNode treeNode2 = new TreeNode(9);
        TreeNode treeNode3 = new TreeNode(20);
        TreeNode treeNode4 = new TreeNode(15);
        TreeNode treeNode5 = new TreeNode(35);

        treeNode1.setLeftTreeNode(treeNode2);
        treeNode1.setRightTreeNode(treeNode3);
        treeNode3.setLeftTreeNode(treeNode4);
        treeNode3.setRightTreeNode(treeNode5);

//        StringBuilder stringBuilder = new StringBuilder();
//        preTraverseBTree(stringBuilder, treeNode1);
//        System.out.println(stringBuilder);

        int[] arrays = {2, 3, 1, 4, 5};

        TreeRoot root = new TreeRoot();
        for (int value : arrays) {
            createTree(root, value);
        }

        System.out.println(preTraverseBTree(root.getTreeRoot()));

        System.out.println(suffixTraverseBTree(root.getTreeRoot()));

        System.out.println(middleTraverseBTree(root.getTreeRoot()));

        System.out.println(getHeight(root.getTreeRoot()));

        System.out.println(getMax(root.getTreeRoot()));
    }

//    public static void preTraverseBTree(StringBuilder stringBuilder, TreeNode rootTreeNode) {
//
//        if (null == rootTreeNode) {
//            return;
//        }
//
//        stringBuilder.append(rootTreeNode.getValue()+"-->");
//
//        preTraverseBTree(stringBuilder,rootTreeNode.getLeftTreeNode());
//
//        preTraverseBTree(stringBuilder, rootTreeNode.getRightTreeNode());
//
//    }

    public static String preTraverseBTree(TreeNode rootTreeNode) {
        if (null == rootTreeNode) {
            return null;
        }

        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append(rootTreeNode.getValue()+"-->");
        String a = preTraverseBTree(rootTreeNode.getLeftTreeNode());
        if (a != null) {
            stringBuilder.append(a);
        }
        String b = preTraverseBTree(rootTreeNode.getRightTreeNode());
        if (b != null) {
            stringBuilder.append(b);
        }

        return stringBuilder.toString();
    }

    public static String suffixTraverseBTree(TreeNode rootTreeNode) {
        if (null == rootTreeNode) {
            return null;
        }

        StringBuilder stringBuilder = new StringBuilder();

        String a = suffixTraverseBTree(rootTreeNode.getLeftTreeNode());
        if (a != null) {
            stringBuilder.append(a);
        }


        String b = suffixTraverseBTree(rootTreeNode.getRightTreeNode());
        if (b != null) {
            stringBuilder.append(b);
        }

        stringBuilder.append(rootTreeNode.getValue()+"-->");


        return stringBuilder.toString();
    }

    public static String middleTraverseBTree(TreeNode rootTreeNode) {
        if (null == rootTreeNode) {
            return null;
        }

        StringBuilder stringBuilder = new StringBuilder();

        String a = middleTraverseBTree(rootTreeNode.getLeftTreeNode());
        if (a != null) {
            stringBuilder.append(a);
        }

        stringBuilder.append(rootTreeNode.getValue()+"-->");


        String b = middleTraverseBTree(rootTreeNode.getRightTreeNode());
        if (b != null) {
            stringBuilder.append(b);
        }



        return stringBuilder.toString();
    }

    public static void createTree(TreeRoot treeRoot, int value) {
        if (treeRoot.getTreeRoot() == null) {
            TreeNode treeNode = new TreeNode(value);
            treeRoot.setTreeRoot(treeNode);
        } else {
            TreeNode tempRoot = treeRoot.getTreeRoot();

            while (tempRoot != null) {
                if (value > tempRoot.getValue()) {
                    if (tempRoot.getRightTreeNode() == null) {
                        tempRoot.setRightTreeNode(new TreeNode(value));
                        return;
                    } else {
                        tempRoot = tempRoot.getRightTreeNode();
                    }
                } else {
                    if (tempRoot.getLeftTreeNode() == null) {
                        tempRoot.setLeftTreeNode(new TreeNode(value));
                        return;
                    } else {
                        tempRoot = tempRoot.getLeftTreeNode();
                    }
                }
            }
        }
    }

    public static int getHeight(TreeNode treeNode) {
        if (null == treeNode) {
            return 0;
        }

        int left = getHeight(treeNode.getLeftTreeNode());

        int right = getHeight(treeNode.getRightTreeNode());

        return left > right ? ++left  : ++right;
    }

    public static int getMax(TreeNode treeNode) {
        if (treeNode == null) {
            return -1;
        } else {
            int left = getMax(treeNode.getLeftTreeNode());
            int right = getMax(treeNode.getRightTreeNode());
            int currentValue = treeNode.getValue();


            return (left > right ? left : right) > currentValue ? (left > right ? left : right) : currentValue;

        }
    }
}
